package com.ja.openchat.ui.chat.adapter.viewholder;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.UiMessage;
import com.zy.openchat.core.model.user.MobileContactInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.ja.openchat.ui.chat.ConversationFragment;
import com.ja.openchat.ui.chat.annotation.EnableContextMenu;
import com.ja.openchat.ui.chat.annotation.MessageContentType;
import com.ja.openchat.views.UserAvatarView;
import com.lxj.xpopup.util.XPopupUtils;

import butterknife.BindView;
import butterknife.OnClick;

@MessageContentType(TelegramManager.MessageType.MessageType_Contact)
@EnableContextMenu
public class ContactContentViewHolder extends NormalMessageContentViewHolder{
    @BindView(R.id.contentTextView)
    TextView contentTextView;
    @BindView(R.id.contact_user_img)
    UserAvatarView contact_user_img;
    @BindView(R.id.sub_title_text)
    TextView sub_title_text;
    @BindView(R.id.content_text_layout)
    LinearLayout content_text_layout;

    public ContactContentViewHolder(ConversationFragment fragment, RecyclerView.Adapter adapter, View itemView) {
        super(fragment, adapter, itemView);
    }

    @OnClick(R.id.contactContentView)
    public void onClick(View view) {
        if(message.message==null||message.message.content==null||message.message.content.contact==null){
            return;
        }
        MobileContactInfo contactInfo = message.message.content.contact;
        UserInfo userInfo = UserInfo.getUserInfo(contactInfo.user_id);
        //群组聊天列表 点击成员头像 20220803
        if(userInfo!=null){
            fragment.onPortraitClick(userInfo);
        }
    }

    @Override
    protected void onBind(UiMessage message) {
       try {
           content_text_layout.getLayoutParams().width = (int)(XPopupUtils.getAppWidth(fragment.getContext())*0.32);
           if(message.message==null||message.message.content==null||message.message.content.contact==null){
               return;
           }
           MobileContactInfo contactInfo = message.message.content.contact;
           UserInfo userInfo = UserInfo.getUserInfo(contactInfo.user_id);
           if(userInfo!=null&&!TextUtils.isEmpty(userInfo.displayName())){
               if(TextUtils.isEmpty(userInfo.username)){
                   sub_title_text.setVisibility(View.GONE);
               }else{
                   sub_title_text.setVisibility(View.VISIBLE);
                   sub_title_text.setText(userInfo.username);
               }

               contact_user_img.showUser(userInfo);
           }else{
               TelegramManager.getTelegramManager().getUserSimpleInfo(contactInfo.user_id);
               sub_title_text.setVisibility(View.GONE);
           }
           contentTextView.setText(contactInfo.displayName());
       }catch (Exception e){
           e.printStackTrace();
       }

    }


}
